package com.mercury.user.controller;


import com.mercury.user.entity.CenterMall;
import com.mercury.user.service.CenterMallService;
import com.mercury.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tong
 * @since 2021-11-22
 */
@RestController
@RequestMapping("/user/center-mall")
@Api(value = "兑换商城模块")
public class CenterMallController {

    @Autowired
    private CenterMallService mallService;

    /**
     * @return
     * @date 2021/11/22 20:31
     * @params
     * @description 获得所有的商城物品
     */
    @GetMapping("/mallList")
    @ApiOperation(value = "获得所有的商城物品")
    public R getMallList() {
        List<CenterMall> list = mallService.list(null);
        return R.ok().data("list", list);
    }

    /**
     * @return
     * @date 2021/11/22 20:31
     * @params id
     * @description 得到商城物品的详细信息
     */
    @GetMapping("mallItem/{id}")
    @ApiOperation(value = "根据商品的 id 获得对应的详情 ")
    public R getMallItem(@PathVariable int id) {
        CenterMall mallItem = mallService.getById(id);
        return R.ok().data("item", mallItem);
    }

    @PostMapping("mallItem/{userId}/{itemId}")
    @ApiOperation(value = "用户兑换商品")
    public R exchangeItem(@PathVariable String userId,
                          @PathVariable int itemId) {
        Boolean result = mallService.exchangeItem(userId, itemId);
        if (result) return R.ok().message("兑换成功");
        else return R.error().message("不好意思，兑换失败了");
    }



}

